﻿using System.Text.RegularExpressions;

namespace uTILLIty.ADExtractor.Core
{
	public class SubAdPropertyInfo
	{
		static readonly Regex SubElementRegex = new Regex(@"(?ixn)^(?<propertyName>[^\[]+)(\[(?<valueIdx>\d+)\])?", RegexOptions.Compiled);
		protected SubAdPropertyInfo()
		{
		}

		public SubAdPropertyInfo(string propertyName)
		{
			PropertyName = propertyName;
		}

		public SubAdPropertyInfo(string propertyName, int singleValueIndex)
		{
			PropertyName = propertyName;
			SingleValueIndex = singleValueIndex;
		}

		public string PropertyName { get; set; }
		public int SingleValueIndex { get; set; }

		public static SubAdPropertyInfo Parse(string property)
		{
			var match = SubElementRegex.Match(property);
			var pi = new SubAdPropertyInfo { PropertyName = match.Groups["propertyName"].Value };
			if (match.Groups["valueIdx"].Success)
				pi.SingleValueIndex = int.Parse(match.Groups["valueIdx"].Value);
			return pi;
		}
	}
}